package com.gingko.duominuo.viewmodel

import androidx.hilt.Assisted
import androidx.hilt.lifecycle.ViewModelInject
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import com.gingko.duominuo.data.Resource
import com.gingko.duominuo.data.asResource
import com.gingko.duominuo.data.request.RegisterReqVo
import com.gingko.duominuo.data.source.repository.GinkgoRepository

class RegisterViewModel @ViewModelInject constructor(
    private val ginkgoRepository: GinkgoRepository,
    @Assisted val state: SavedStateHandle
) : BaseViewModel() {
    private val _sendCodeLiveData = MutableLiveData<Resource<String>>()
    val sendCodeLiveData: LiveData<Resource<String>> = _sendCodeLiveData
    private val _registerLiveData = MutableLiveData<Resource<String>>()
    val registerLiveData: LiveData<Resource<String>> = _registerLiveData

    fun register(registerReqVo: RegisterReqVo) {
        launchOnUI(
            task = {
                _registerLiveData.value = ginkgoRepository.register(registerReqVo).asResource()
            },
            onFailure = {
                _registerLiveData.value = Resource.Error(it)
            }
        )
    }

    fun sendSmsCode(phoneNumber: String) {
        launchOnUI(
            task = {
                _sendCodeLiveData.value = ginkgoRepository.sendSMSCode(phoneNumber).asResource()
            }, onFailure = {
                _sendCodeLiveData.value = Resource.Error(it)
            }
        )
    }

}